<?php
/**
 * 聚会列表
 */
namespace Meeting\Controller;
use Common\Controller\AdminbaseController;

class AdminMeetingController extends AdminbaseController{

	protected $meeting_model;
	protected $meeting_cate_model;
	protected $meeting_comment_model;
	protected $meeting_joined_model;

	function _initialize()
	{
		parent::_initialize();
		$this->meeting_model = D("Meeting");
		$this->meeting_cate_model = D("MeetingCate");
		$this->meeting_comment_model = D("MeetingComment");
		$this->meeting_joined_model = D("MeetingJoined");
	}

	// 聚会列表
	public function index($status = 'doing'){

		// 条件1 - 开始时间-结束时间
		date_default_timezone_set("PRC");
		$time = time();//当前时间
		//doing 正在进行中
		$where_doing['meeting_start_time'] 		= array('ELT',$time);
		$where_doing['meeting_end_time']			= array('EGT',$time);
		$where_doing['status'] = 1;
		//end 已结束
		$where_end['meeting_end_time'] 			= array('LT',$time);
		$where_end['status'] = 1;
		// notbegin 未开始
		$where_notbegin['meeting_start_time'] 	= array('GT',$time);
		$where_notbegin['status'] = 1;
		// nopass
		$where_notpass['status'] =0;

		// 四大条件：聚会正在进行中（报名情况 doing），聚会已结束（报名情况+导出数据 end），聚会未开始(notbging)， 审核未通过(notpass)
		$status == 'all'		&& $where = array(); //默认全部
		$status == 'doing' 	&& $where = $where_doing; 		// 聚会正在进行
		$status == 'end' 		&& $where = $where_end; 		// 聚会已经结束
		$status == 'notbegin'	&& $where = $where_notbegin; 	// 聚会未开始
		$status == 'notpass' 	&& $where = $where_notpass; 	// 聚会未通过

		$start_time = strtotime(I('start_time'));
		$end_time = strtotime(I('end_time'));
		if($start_time && $end_time){
			$where_doing['meeting_start_time'] 		= array('ELT',$start_time);
			$where_doing['meeting_end_time']			= array('EGT',$end_time);
		}

		// 条件2 - 聚会标题
		$meeting_title = I('meeting_title');
		if($meeting_title){
			$where['meeting_title'] = array('like',"%$meeting_title%");
		}

		// 条件3 - 聚会分类名称
		$meeting_cid = I('meeting_cid');
		if($meeting_cid) {
			$where['meeting_cid'] = $meeting_cid;
		}
		$this->assign('meeting_cid',$meeting_cid);	 // 分类选中值

		// 聚会分类
		$cate = $this->meeting_cate_model
			->order('orderby asc')
			->select();
		$this->assign('cate',$cate);


		// 数量
		$db_prefix=C('DB_PREFIX');
		$count= $this->meeting_model
			-> alias('a')
			-> join($db_prefix.'meeting_cate  b  ON a.meeting_cid = b.cate_id')
			-> join($db_prefix.'users c on a.user_id = c.id')
			-> join($db_prefix.'shop d on a.address_id = d.shop_id')
			-> where($where)
			-> count();

		// 分页
		$page = $this->page($count, 10);

		$lists = $this->meeting_model
			-> alias('a')
			-> field('a.*,b.cate_name,c.user_nicename,d.shop_name')
			-> join($db_prefix.'meeting_cate  b  ON a.meeting_cid = b.cate_id')
			-> join($db_prefix.'users c on a.user_id = c.id')
			-> join($db_prefix.'shop d on a.address_id = d.shop_id')
			-> where($where)
			-> order('a.meeting_id desc')
			-> limit($page->firstRow, $page->listRows)
			-> select();

		$this -> assign("page", $page->show('Admin'));
		$this -> assign('lists',$lists);
		$this -> assign('status',$status);
		$this -> display('index');
	}

	// 查看聚会详情
	public function details(){
		$meeting_id = I('meeting_id/d');

		// 聚会详情
		$where['meeting_id'] = $meeting_id;
		$db_prefix=C('DB_PREFIX');
		$meeting_details = $this->meeting_model
								->alias('a')
								->field('a.*,b.cate_name ,c.user_nicename,d.shop_name')
								->join($db_prefix.'meeting_cate b on a.meeting_cid=b.cate_id')
								->join($db_prefix.'users c on a.user_id = c.id')
								->join($db_prefix.'shop d on a.address_id = d.shop_id')
								->where($where)
								->find();

		// 聚会参加的人
		$meeting_joined_list = $this->meeting_joined_model
									->alias('a')
									->field('a.*,b.user_nicename')
									->join($db_prefix.'users b on a.user_id = b.id')
									->where($where)
									->select();

		// 聚会评论人员列表
		$meeting_comment_list = $this->meeting_comment_model
										->alias('a')
										->field('a.*,b.user_nicename')
										->join($db_prefix.'users b on a.user_id = b.id')
										->where($where)
										->select();

		$this->assign('meeting_details'		,$meeting_details); 		//聚会详情
		$this->assign('meeting_comment_list'	,$meeting_comment_list); 	//聚会评论
		$this->assign('meeting_joined_list'	,$meeting_joined_list); 	//聚会参加
		$this->display();
	}

	// 删除
	public function delete() {
		$meeting_id = I('meeting_id/d');
		$where['meeting_id'] = $meeting_id;
		$result = $this->meeting_model->where($where)->delete();
		$data = array(
			'status' => $result ? true : false,
			'info' => $result ? '' : '删除失败，ID：'.$meeting_id,
			'url' => '',
		);
		$this -> ajaxReturn($data);
	}

	/**
	 * 批量操作
	 * @param string $type    处理类型 order排序
	 */
	public function indexBat($type='delete') {
		// 批量删除
		if ($type == 'delete') {
			$ids = I('post.ids');
			if ($this->meeting_model -> delete(implode(',',$ids)) !== FALSE) {
				$this -> success('批量删除成功');
			} else {
				$this -> error('删除失败');
			}
		}
		// 批量驳回
		elseif ($type == 'reject') {
			$ids = I('post.ids');
			$update_array = array();
			foreach ($ids as $id) {
				$update_array[] = array('meeting_id' => $id, 'status' => '0');
			}
			if ($this->meeting_model->saveBatch($update_array, 'meeting_id') !== FALSE) {
				$this->success('驳回成功');
			} else {
				$this->error('驳回失败');
			}
		}
		// 批量审核
		elseif ($type == 'pass') {
			$ids = I('post.ids');
			$update_array = array();
			foreach ($ids as $id) {
				$update_array[] = array('meeting_id' => $id, 'status' => '1');
			}
			if ($this->meeting_model->saveBatch($update_array, 'meeting_id') !== FALSE) {
				$this->success('驳回成功');
			} else {
				$this->error('驳回失败');
			}
		}

	}


}